package manager;

import gate.util.GateException;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import persistence.Axdbv4HibernateManager;
import persistence.DrupalHibernateManager;

public class Main {

	/**
	 * @param args
	 * @throws Exception 
	 * @SuppressWarnings
	 * {@link SuppressWarnings}
	 */
	public static void main(String[] args) throws Exception {
		/*PrintStream oldErr = System.err;
		PrintStream newErr = new PrintStream(new ByteArrayOutputStream());
		System.setErr(newErr);
*/
		
		Logger.getRootLogger().setLevel(Level.OFF); //sopprime i warning di log4j
		
		Integer input = 0;
		String inputAddr=null;
		String inputUsr=null;
		String inputPass=null;
		System.out.println("\t\t\t========USAGE INFO========\n\nPlease, enter 0 args to start the interactive mode or ");
//		System.out.println("If you enter 1 arg it will be started directly phase1 or phase3 on interactive mode.");
//		System.out.println("args[0]={1,2} (1 for Phase1, 2 for Phase3)");
//		System.out.println("args[1]")
		
		System.out.println("\n\n\tEnter \"1 DBAddredd(:port) DBUserName DBPass Y\" to start Phase1\tY=number of record to use");
		System.out.println("\n\n\tEnter \"2 DBAddredd(:port) DBUserName DBPass X Y Z\" to start Phase3\n\tX={1,2,3}\n\t\t1-Search person inside Eclap users tables\n\t\t2-Search person using DBpedia\n\t\t3-Search person in Eclap AND DBpedia\n\tY=Number of record to use\n\tZ (optional). If empty, the standard value will be http://dbpedia.org/sql. Otherwise it will be taken as the DBpedia Service (eg. \"http://it.dbpedia.org/sql\"");
		System.out.println("\n\tFor Example:\t 3 jdbc:mysql://localhost root password 3 5 http://it.dbpedia.org/sparql");
		System.out.println("\t\t\t 3 jdbc:mysql://localhost root password 3 5");
		System.out.println("\t\t\t 1 jdbc:mysql://localhost root password 5");
		
		if (args.length<4){
			while (true){
				/*System.out.println(new File("./").getCanonicalPath());
				
				System.out.println(new File("..").getCanonicalPath());
				System.out.println(new File("../..").getCanonicalPath());
				System.out.println(new File("").getCanonicalPath());
				System.out.println(new File("/").getCanonicalPath());
				System.out.println(new File("./lib/gate").getCanonicalPath());
				*/
				BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		        System.out.print("\nWelcome in interactive mode! Select:\n\t1. Phase1 (Find Names on Eclap DB) \n\t3. Phase3 (Match persons by name) ");
		        input = null;
		        try {
		            input = Integer.valueOf(reader.readLine());
		            
		        } catch (IOException e) {
		            e.printStackTrace();
		        } catch ( java.lang.NumberFormatException e) {
		        	input=0;
				}
		        
		    	
		    	System.out.println("Now insert the DB address (eg. jdbc:mysql://localhost, jdbc:mysql://localhost:3306, ...)");
		    	inputAddr = reader.readLine();
		    	System.out.println("Now insert the DB user name (eg. root)");
		    	inputUsr = reader.readLine();
		    	System.out.println("Now insert the DB user password");
		    	//if (System.console() == null) {
		    	inputPass = reader.readLine();
		       /* }else{
		        	char[] pass=System.console().readPassword("*");	
		        	inputAddr = String.valueOf(pass);
		        }
		    	*/
		    	if ((input==1 || input==3) && inputAddr!=null && inputUsr!=null && inputPass!=null) break;
		    	System.out.println("Please, insert the correct values!");
			}
			Axdbv4HibernateManager.setHost(inputAddr);
			Axdbv4HibernateManager.setUser(inputUsr);
			Axdbv4HibernateManager.setPassword(inputPass);
			DrupalHibernateManager.setHost(inputAddr);
			DrupalHibernateManager.setUser(inputUsr);
			DrupalHibernateManager.setPassword(inputPass);
			
			switch (input) {
			case 1:
				
				Phase1.main(args);
				break;
			case 3:
				Phase3.main(args);
				
				break;	
			

			default:
				System.out.println("You enter wrong inputs");
				break;
			}

		}else if(args.length<5){
			
			Axdbv4HibernateManager.setHost(args[1]);
			Axdbv4HibernateManager.setUser(args[2]);
			Axdbv4HibernateManager.setPassword(args[3]);
			DrupalHibernateManager.setHost(args[1]);
			DrupalHibernateManager.setUser(args[2]);
			DrupalHibernateManager.setPassword(args[3]);
			
			if(Integer.valueOf(args[0])==1) {Phase1.main(Arrays.copyOfRange(args, 4, args.length)); return;}
			if(Integer.valueOf(args[0])==3) {Phase3.main(Arrays.copyOfRange(args, 4, args.length)); return;}
			System.out.println("\t\t\t========USAGE INFO========\n\nPlease, enter 0 args to start the interactive mode or ");
//			System.out.println("If you enter 1 arg it will be started directly phase1 or phase3 on interactive mode.");
//			System.out.println("args[0]={1,2} (1 for Phase1, 2 for Phase3)");
//			System.out.println("args[1]")
			
			System.out.println("\n\n\tEnter \"1 DBAddredd(:port) DBUserName DBPass Y\" to start Phase1\tY=number of record to use");
			System.out.println("\n\n\tEnter \"2 DBAddredd(:port) DBUserName DBPass X Y Z\" to start Phase3\n\tX={1,2,3}\n\t\t1-Search person inside Eclap users tables\n\t\t2-Search person using DBpedia\n\t\t3-Search person in Eclap AND DBpedia\n\tY=Number of record to use\n\tZ (optional). If empty, the standard value will be http://dbpedia.org/sql. Otherwise it will be taken as the DBpedia Service (eg. \"http://it.dbpedia.org/sql\"");
			System.out.println("\n\tFor Example:\t 3 jdbc:mysql://localhost root password 3 5 http://it.dbpedia.org/sparql");
			System.out.println("\t\t\t 3 jdbc:mysql://localhost root password 3 5");
			System.out.println("\t\t\t 1 jdbc:mysql://localhost root password 5");
			return;
		}if (args.length>=5){
			
			Axdbv4HibernateManager.setHost(args[1]);
			Axdbv4HibernateManager.setUser(args[2]);
			Axdbv4HibernateManager.setPassword(args[3]);
			DrupalHibernateManager.setHost(args[1]);
			DrupalHibernateManager.setUser(args[2]);
			DrupalHibernateManager.setPassword(args[3]);
			
			
			
			if(Integer.valueOf(args[0])==1) {Phase1.main(Arrays.copyOfRange(args, 4, args.length)); return;}
			if(Integer.valueOf(args[0])==3 && args.length>=6) {Phase3.main(Arrays.copyOfRange(args, 4, args.length)); return;}
			
		}
	
		

				//System.setErr(oldErr);
	}
}
